﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;

namespace MINERP.InfoConfig.Version
{
    public  class VersionManager
    {
        private static string FilePath = Application.StartupPath + @"\Version.db";
        public void IniDB(string version,string updateFilePath)
        {
            if (File.Exists(FilePath))
            {
                File.Delete(FilePath);
            }
            DataTable dt = CreateTable();
            DataRow dr = dt.NewRow();
            dr["newVersion"] = version;
            dr["UpdateFilePath"] = updateFilePath;
            dt.Rows.Add(dr);
            dt.WriteXml(FilePath,XmlWriteMode.WriteSchema);
            dt.Dispose();
        }

        public void  GetVersionNum(out string version,out string path)
        {

            version = string.Empty;
            path = string.Empty;
            if (File.Exists(FilePath))
            {

                DataRow[] rows;
                DataTable dt = new DataTable("versionInfo");
                dt.ReadXml(FilePath);
                rows = dt.Select();
                version = rows.ToList()[0]["newVersion"].ToString();
                path = rows.ToList()[0]["UpdateFilePath"].ToString();

                dt.Dispose();
            }
        }

        private DataTable CreateTable()
        {
            DataTable dt = new DataTable("versionInfo");
            dt.Columns.Add("newVersion", typeof(string));
            dt.Columns.Add("UpdateFilePath", typeof(string));
            return dt;
        }
    }
}
